/*
 * ******************************************************
 *   Copyright ©, 2017-2019, 安徽财金信息科技有限公司
 * ******************************************************
 */
package com.wy.poties.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.wy.poties.constant.ResultCode;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


@Data
@NoArgsConstructor
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = -8208289451429268805L;

    private Integer code;
    private String msg;
    private T data;

    public ApiResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> ApiResult<T> success() {
        ApiResult<T> result = new ApiResult<>();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> result = new ApiResult<>();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    public static <T> ApiResult<T> failure(ResultCode resultCode) {
        ApiResult<T> result = new ApiResult<>();
        result.setResultCode(resultCode);
        return result;
    }

    public static <T> ApiResult<T> failure(ResultCode resultCode, T data) {
        ApiResult<T> result = new ApiResult<>();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }


    public static <T> ApiResult<T> failure(String msg) {
        ApiResult<T> result = new ApiResult<>();
        result.setCode(1);
        result.setMsg(msg);
        return result;
    }

    @JsonIgnore
    public void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.message();
    }


}
